package com.example.demo.config.redis_shiro;

import com.example.demo.constant.RedisConstant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionIdGenerator;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.UUID;

/**
 * fileName:CustomSessionIdGenerator
 * description:
 * author: LJV
 * createTime:2022/4/20 11:57
 * version:1.0.0
 */
@Component
public class CustomSessionIdGenerator implements SessionIdGenerator {
    @Override
    public Serializable generateId(Session session) {
        ServletRequestAttributes requestAttr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest httpServletRequest = requestAttr.getRequest();
        String tenantHeader = httpServletRequest.getHeader("tenant");
        return tenantHeader + RedisConstant.SPLIT + UUID.randomUUID();
    }
}
